package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUITree;
import com.ego.commons.pojo.EgoResult;
import com.ego.dubbo.service.TbContentCategoryDubboService;
import com.ego.pojo.TbContentCategory;
import com.ego.service.TbContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
public class TbContentCategoryServiceImpl implements TbContentCategoryService {
    @DubboReference
    private TbContentCategoryDubboService tbContentCategoryDubboService;
    @Override
    public List<EasyUITree> tbContentCategoryList(long id) {

        List<EasyUITree> easyUITrees = tbContentCategoryDubboService.selectByPid(id);
        return easyUITrees;
    }

    @Override
    public EgoResult tbContentCategoryCreate(long parentId,String name) {
        TbContentCategory contentCategory = new TbContentCategory();

        Date now = new Date();
        contentCategory.setParentId(parentId);
        contentCategory.setName(name);
        contentCategory.setCreated(now);
        contentCategory.setUpdated(now);
        contentCategory.setStatus(1);
        contentCategory.setIsParent(false);

        try {
            int i = tbContentCategoryDubboService.insertContentCategory(contentCategory);
            if (i==1){
               return EgoResult.ok(contentCategory);
            }
        } catch (DaoException e) {
            e.printStackTrace();
          return   EgoResult.error(e.getMessage());
        }
        return EgoResult.error("添加失败");
    }

    @Override
    public EgoResult contentCategoryUpdate(TbContentCategory tbContentCategory) {
        tbContentCategory.setUpdated(new Date());

        try {
            int i = tbContentCategoryDubboService.updateName(tbContentCategory);
            if (i==1){
                return EgoResult.ok(tbContentCategory);
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("改名失败，目录名已存在");
    }

    @Override
    public EgoResult contentCategoryDel(long id) {

        try {
            int i = tbContentCategoryDubboService.deleteById(id);
            if (i==1){
              return EgoResult.ok();
            }else {
                return EgoResult.error("删除失败，存在子目录");
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
    }
}
